﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bowerbird.Core.Storage;
using Bowerbird.Core.Utils;
using Dto = Bowerbird.Api.Dtos;

namespace Bowerbird.Api
{
    public class UserService
    {
        public Dto.User LogIn(string identityProvider, string nameIdentifier, string name)
        {
            User user = UserRepository.GetByNameIdentifier(identityProvider, nameIdentifier);

            if (user == null)
            {
                user = UserRepository.Register(identityProvider, nameIdentifier, name);
            }

            return new Dto.User
            {
                id = Urn.Create(UrnResource.User, user.Id),
                link = Link.Create(LinkRel.Self, UrnResource.User, user.Id),
                email = user.Email,
                name = user.Name,
                identityProvider = user.IdentityProvider,
                nameIdentifier = user.NameIdentifier,
                profilePicture = user.ProfilePictureUrl
            };
        }

        public Dto.User Edit(Dto.User dto)
        {
            string id = Urn.GetIdentifier(dto.id);

            User user = RepositoryBase<User>.GetById(id);

            if (user != null)
            {
                user.Name = dto.name;
                user.Email = dto.email;
                user.ProfilePictureUrl = dto.profilePicture;

                user = RepositoryBase<User>.SaveOrUpdate(user);
            }
            else
            {
                throw new ArgumentException("User Not found for the specified id", "id");
            }

            return new Dto.User
            {
                id = Urn.Create(UrnResource.User, user.Id),
                link = Link.Create(LinkRel.Self, UrnResource.User, user.Id),
                email = user.Email,
                name = user.Name,
                identityProvider = user.IdentityProvider,
                nameIdentifier = user.NameIdentifier,
                profilePicture = user.ProfilePictureUrl
            };
        }

        public Dto.ContactData AddNewContactData(string userId, int contactDataType, string name, string value, string visibleTo)
        {
            ContactData contactData = UserRepository.AddNewContactData(userId, contactDataType, name, value, visibleTo);

            return Assembler<ContactData, Dto.ContactData>.ToDto(contactData);
        }

        public IEnumerable<Dto.ContactData> GetContactData(string userId)
        {
            IEnumerable<ContactData> contactData = UserRepository.GetContactData(userId);

            return Assembler<ContactData, Dto.ContactData>.ToDto(contactData);
        }

        public void RemoveContactData(string userId, string id)
        {
            UserRepository.RemoveContactData(userId, id);
        }

        public IEnumerable<Dto.User> SuggestUsers(string userId)
        {
            var users = UserRepository.GetAll().Where(u => u.Id != userId).AsEnumerable();

            return Assembler<User, Dto.User>.ToDto(users);
        }

    }
}
